package cn.gao.utils;

import cn.gao.constant.FileTidyConstant;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: GaoXu
 * @Date: 2022/05/20/15:21
 * @Description:
 */
@Slf4j
public class FileUtils {
    public static void main(String[] args) {
        System.out.println(fileCut(FileTidyConstant.LISTENFLODERNAME + "\\dc-icon-人工智能.png", "E:\\下载测试\\图片文件夹\\dc-icon-人工智能.png"));
    }

    public static boolean fileCut(String sourcePath,String targetPath){
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        if (fileCopy(sourceFile, targetFile)){
            return FileUtil.del(sourceFile);
        }
        return false;
    }


    public static boolean fileCopy(String sourcePath, String targetPath) {
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        return fileCopy(sourceFile, targetFile);
    }

    public static boolean fileCopy(File source, File target) {
        boolean flag = false;
        try {
            FileInputStream inputStream = new FileInputStream(source);
            FileOutputStream outputStream = new FileOutputStream(target);
            IoUtil.copy(inputStream, outputStream);
            flag = true;
        } catch (IOException e) {
            log.error("IOException e:{}", e);
        }

        return flag;
    }

    /**
     * 获取文件前缀
     *
     * @param file
     * @return
     */
    public static String getFilePreName(File file) {
        if (file.isDirectory()) {
            return file.getName();
        }
        String fileName = file.getName();

        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    /**
     * 获取文件前缀
     *
     * @param fileName
     * @return
     */
    public static String getFilePreName(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    /**
     * 获取文件后缀
     *
     * @param file
     * @return
     */
    public static String getFileSuffName(File file) {

        if (file.isDirectory()) {
            return file.getName();
        }
        String fileName = file.getName();

        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 获取文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileSuffName(String fileName) {

        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
}
